package entity;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

/**
 *
 * @author Francis
 */
public class BookService {

    private static EntityManagerFactory emf = EntityManagerHelper.getFactory();

    public static List<Book> getByCategory(String categoryID) {
        EntityManager em = emf.createEntityManager();
        try {
            int id = Integer.parseInt(categoryID);
            Query query = em.createNamedQuery("Book.findByCategory").setParameter("category", id);
            List<Book> books = (List<Book>) query.getResultList();
            return books;
        } catch (Exception e) {
            //throw e;
            //item not found
        } finally {
            em.close();
        }
        return null;
    }

    public static Book getById(String id) {
        EntityManager em = emf.createEntityManager();
        try {
            int itemID = Integer.parseInt(id);
            Query query = em.createNamedQuery("Book.findByBookID").setParameter("bookID", itemID);
            Book b = (Book) query.getSingleResult();
            return b;
        } catch (Exception e) {
            //throw e;
            //item not found
        } finally {
            em.close();
        }
        return null;
    }

    public static List<Book> getByTag(String tag) {
        EntityManager em = emf.createEntityManager();
        try {
            Query query = em.createNamedQuery("Book.findByTag").setParameter("tag", tag);
            List<Book> b = (List<Book>)query.getResultList();
            return b;
        } catch (Exception e) {
            //throw e;
            //item not found
        } finally {
            em.close();
        }
        return null;
    }

    public static void incrementClicksById(String id) {
        EntityManager em = emf.createEntityManager();
        try {
            int itemID = Integer.parseInt(id);
            em.getTransaction().begin();
            Query query = em.createNamedQuery("Book.IncrementClicksByBookID").setParameter("bookID", itemID);
            query.executeUpdate();
            em.getTransaction().commit();
        } catch (Exception e) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
        } finally {
            em.close();
        }
    }

    public static Book incrementClicksAndThenGetById(String id) {
        EntityManager em = emf.createEntityManager();
        try {
            int itemID = Integer.parseInt(id);
            em.getTransaction().begin();
            Query query = em.createNamedQuery("Book.IncrementClicksByBookID").setParameter("bookID", itemID);
            query.executeUpdate();
            em.getTransaction().commit();
            Query query2 = em.createNamedQuery("Book.findByBookID").setParameter("bookID", itemID);
            Book b = (Book) query2.getSingleResult();
            return b;
        } catch (Exception e) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
        } finally {
            em.close();
        }
        return null;
    }

    public static boolean persist(Book object) {
        return EntityManagerHelper.persist(object, emf);
    }
}
